import { _decorator, Button, Label, Tween, tween, UITransform, Widget } from 'cc';
import { DataProxy } from '../../../frame/A-LIB/lib.b.data';
import { BaseUI } from '../../../frame/component/ui.base';
import GGameDC from '../../manager/game.data.game.center';
import GGameAssets from '../../manager/game.game.assets';
import { GPopup } from './game.popup';
const { ccclass, property } = _decorator;

@ccclass('GHomeUI')
export class GHomeUI extends BaseUI {

	private ui_top: UITransform;
	private ui_bottom: UITransform;
	private va_top: mtec.cc.VecAssist;
	private va_bottom: mtec.cc.VecAssist;

	protected initAfterOnLoad(){
		this.ui_top = this.node.getChildByName('top').getComponent(UITransform);
		this.ui_bottom = this.node.getChildByName('bottom').getComponent(UITransform);
		this.va_top = new mtec.cc.VecAssist(this.ui_top.node);
		this.va_bottom = new mtec.cc.VecAssist(this.ui_bottom.node);

		this.renderLocalUI();
	}

	protected start(){
		let wgt = this.node.getComponent(Widget);
		if(wgt) wgt.updateAlignment();

		this.va_top.getPosition('center', 'over_top');
		this.va_bottom.getPosition('center', 'over_bottom');
		this.ui_top.node.setPosition(this.va_top.vec);
		this.ui_bottom.node.setPosition(this.va_bottom.vec);
	}

	protected _click_event_: { [name: string]: (this: GHomeUI, button: Button) => void; } = {
		setting(btn){
			if(GPopup.lock){
				return void 0;
			}
			GPopup.lock = true;
			btn.interactable = false;
			GPopup.Setting().then(()=>{
				btn.interactable = true;
				GPopup.lock = false;
			});
		},
		async stash(){
			this.enablePropBtn(false);
			GGameDC.Trigger.STASH = false;
			this.enablePropBtn(true);
		},
		async revoke(){
			this.enablePropBtn(false);
			GGameDC.Trigger.REVOKE = false;
			this.enablePropBtn(true);
		},
		async shuffle(){
			this.enablePropBtn(false);
			GGameDC.Trigger.SHUFFLE = false;
			this.enablePropBtn(true);
		}
	};

	protected listening(){

		let list = [
			DataProxy.follow(GGameDC.Trigger, 'UIHome', (_, show)=>{
				if(_==show) return void 0;
				if(show) this.enter();
				else this.leave();
			}, false)
		];
		return list;
	}

	private enter(duration?: number){
		this.node.active = true;
		duration = duration ?? 0.5;

		this.va_top.getPosition('center', 'top');
		if(mtec.cc.is_long_screen) this.va_top.vec.add3f(0, -100, 0);

		Tween.stopAllByTarget(this.ui_top.node);
		tween(this.ui_top.node)
		.to(duration, {position: this.va_top.vec}, {easing: 'cubicOut'})
		.start();

		this.va_bottom.getPosition('center', 'bottom');
		Tween.stopAllByTarget(this.ui_bottom.node);
		tween(this.ui_bottom.node)
		.to(duration, {position: this.va_bottom.vec}, {easing: 'cubicOut'})
		.start();
	}

	private leave(duration?: number){
		duration = duration ?? 0.5;

		this.va_top.getPosition('center', 'over_top');

		Tween.stopAllByTarget(this.ui_top.node);
		tween(this.ui_top.node)
		.to(duration, {position: this.va_top.vec}, {easing: 'cubicIn'})
		.call(()=>this.node.active = false)
		.start();

		this.va_bottom.getPosition('center', 'over_bottom');
		Tween.stopAllByTarget(this.ui_bottom.node);
		tween(this.ui_bottom.node)
		.to(duration, {position: this.va_bottom.vec}, {easing: 'cubicIn'})
		.start();

		if(GGameDC.User.round) GGameDC.Trigger.UILuckyBox = false;
	}

	private renderLocalUI(){
		(<const>[
			['stash', GGameAssets.getLangText(10)],
			['revoke', GGameAssets.getLangText(11)],
			['shuffle', GGameAssets.getLangText(12)]
		]).forEach(([name, text])=>this.ui_bottom.node.getChildByName('lbl-'+name).getComponent(Label).string = text);
	}

	private enablePropBtn(enable: boolean){
		['stash', 'revoke', 'shuffle'].forEach(name=>this.__btn_map__.get(name).interactable = enable);
	}
}

